<template>
  <div id="resourceList">

    <div class="flex_head">
      <div class="solid"></div>
      <div class="item_two">
        <el-button type="primary" size="small" @click="changeStatus()">添加资源</el-button>
      </div>
    </div>
    <el-dialog title="保存资源" :visible.sync="dialogFormVisible" size="tiny">
      <el-form :model="form">
        <!--<el-form-item label="资源名" :label-width="formLabelWidth">-->
        <!--<el-input v-model="form.resource" auto-complete="off"></el-input>-->
        <!--</el-form-item>-->
        <el-form-item label="方法" :label-width="formLabelWidth">
          <el-select v-model="form.method" clearable placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="路径" :label-width="formLabelWidth">
          <el-input v-model="form.path" auto-complete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveResource">确 定</el-button>
      </div>
    </el-dialog>
    <el-table :data="tableData" stripe style="width: 100%">
      <!--<el-table-column prop="resource" label="资源名">-->
      <!--</el-table-column>-->
      <el-table-column prop="method" label="方法">
      </el-table-column>
      <el-table-column prop="path" label="路径">
      </el-table-column>

      <el-table-column label="修改">
        <template slot-scope="scope">
          <el-button type="text" @click="changeStatus2(scope.row)">修改</el-button>
        </template>
      </el-table-column>
      <el-table-column label="删除">
        <template slot-scope="scope">
          <el-button type="text" @click="deleteResource(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
      <el-table-column label="权限编辑">
        <template slot-scope="scope">
          <el-button type="text" @click="editorOpen(scope.row.id)">权限编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="text-align: right;padding-right: 20px">

      <el-pagination layout="prev, pager, next, total" @current-change="handleCurrentChange"
                     :current-page.sync="pageNumber" :page="pageNumber" :total="total"
                     :page-size="pageSize">
      </el-pagination>

    </div>
    <el-dialog title="资源权限编辑" :visible.sync="dialogVisible" size="small">
      <el-transfer v-model="permissionInfo.permissionId" :data="data" style="text-align: left"></el-transfer>
      <span slot="footer" class="dialog-footer" style="text-align: center">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="editorResource">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        tableData: [{
          resource:'sdasd',
          method:"GET",
          path:'dasdas'
        }],
        options: [
          {
            value: 'ALL',
            label: 'ALL'
          },
          {
          value: 'GET',
          label: 'GET'
        }, {
          value: 'POST',
          label: 'POST'
        }, {
          value: 'DELETE',
          label: 'DELETE'
        }, {
          value: 'PUT',
          label: 'PUT'
        }],
        pageSize: 10,
        pageNumber: 1,
        total: 1,
        dialogFormVisible: false,
        dialogVisible:false,
        form: {
//          resource: '',
          method: '',
          path: ''
        },
        formLabelWidth: '80px',
        permissionInfo:{
          permissionId:[],
          resourceId:''
        },
        data:[]

      }
    },
    created() {
      this.getResourceList()
    },
    methods: {
      getResourceList() {
        let params = {
          pageSize: this.pageSize,
          pageNumber: this.pageNumber
        }
        this.$api.getResourceList(params).then(res => {
          this.$message.success("获取资源列表成功");
          this.tableData = res.data.list;
          this.total = res.data.totalRow
        })
      },
      changeStatus() {
        this.dialogFormVisible = true;
        sessionStorage.setItem("resource id", '');

        this.form.method = '';
        this.form.path = '';
      },
      changeStatus2(row) {
        this.dialogFormVisible = true;
        sessionStorage.setItem("resource id", row.id);

        this.form.method = row.method;
        this.form.path = row.path;
      },
      saveResource() {
        this.dialogFormVisible = false;
        let id = sessionStorage.getItem("resource id");
        if (id) {
          this.form.id = id
        } else {
          if (this.form.id) {
            delete this.form.id
          }
        }
        this.$api.saveResource(this.form).then(res => {
          this.$message.success("保存资源成功");
          this.getResourceList()
        })
      },
      deleteResource(id) {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$api.deleteResource(id).then(res => {
            this.$message.success("删除资源成功");
            this.getResourceList()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });

      },
      editorOpen(id){
        this.dialogVisible = true;
        this.permissionInfo.resourceId =id;
        this.$api.getResourcePermissionList(id).then(res=>{
          this.$message.success("获取资源权限列表成功");
          this.permissionInfo.permissionId = res.data.checkedKeys
          this.data=res.data.data
        })
      },
      editorResource(){
        this.dialogVisible = false;
         this.$api.aditorPermission(this.permissionInfo).then(res=>{
           this.$api.success("保存成功")
         })
      },
      handleCurrentChange(val){
        this.pageNumber  =val;
        this.getResourceList()
      }
    }
  }
</script>

<style lang="less">
  #resourceList {
    th {
      text-align: center;
    }
    .el-dialog__footer{
      text-align: center;
    }
  }
</style>
